package com.kaibo.redis.queue.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * @author: kaibo
 * @date: 2021/1/9 14:26
 * @GitHub: https://github.com/yuxuelian
 * @qq: 568966289
 * @description:
 */

@Slf4j
@Component
public class MessageConsumer {

    public static final String MESSAGE_TEST = "MESSAGE_TEST";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Async
    public void onMessage() {
        while (true) {
            try {
                ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
                String message = listOperations.rightPop(MESSAGE_TEST, Duration.ofHours(10));
                log.info("message = {}", message);
            } catch (Exception e) {
                log.error("MessageConsumer onMessage {}", e.getMessage());
            }
        }
    }

}
